<template>
  <div>
    <el-card>
      <!-- 搜索添加 -->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoods">保存</el-button>
        </el-col>
      </el-row>
      <el-alert
        title="秒杀中的商品"
        type="success"
        center
        show-icon
        :closable="false"
        style="margin-top:20px"
      ></el-alert>
      <el-table :data="goods" border stripe>
        <el-table-column label="id" prop="id" width="55px"></el-table-column>
        <el-table-column label="商品名称" prop="goods_title" width="700px"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price" width="180px"></el-table-column>
        <el-table-column label="秒杀价格" prop="sk_price" width="180px"></el-table-column>
        <el-table-column label="秒杀数量" prop="sk_num" width="180px"></el-table-column>
        <el-table-column label="限购" prop="limit_num" width="150px"></el-table-column>
        <el-table-column label="操作" width="175px">
          <template slot-scope="scope">
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="remove(scope.row.goods_id)"
              size="mini"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-alert
        title="添加秒杀商品"
        type="warning"
        center
        show-icon
        :closable="false"
        style="margin-top:20px"
      ></el-alert>
      <el-form>
        <!-- table表格区域 -->
        <el-table
          :data="goodslist"
          border
          stripe
          :row-key="getRowKey"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" :reserve-selection="true"></el-table-column>
          <el-table-column label="商品名称" prop="goods_title" width="700px"></el-table-column>
          <el-table-column label="商品价格(元)" prop="goods_price" width="180px"></el-table-column>

          <el-table-column label="秒杀价格" prop="sk_price" width="180px">
            <template slot-scope="scope">
              <el-input v-model="scope.row.sk_price" placeholder="请输入秒杀价格"></el-input>
            </template>
          </el-table-column>

          <el-table-column label="秒杀数量" prop="sk_num" width="180px">
            <template slot-scope="scope">
              <el-input v-model="scope.row.sk_num" placeholder="数量"></el-input>
            </template>
          </el-table-column>

          <el-table-column label="限购数量" prop="limit_num" width="150px">
            <template slot-scope="scope">
              <el-input v-model="scope.row.limit_num" placeholder="限购数量"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="175px"></el-table-column>
        </el-table>
      </el-form>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1,2,5,10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      ></el-pagination>
    </el-card>
  </div>
</template>
<script>
import { server } from '../api/index'
//请求接口然后渲染再勾选填写
export default {
  data() {
    return {
      //查询对象
      queryInfo: {
        query: '',
        //当前页数
        pagenum: 1,
        //当前页显示多少条

        pagesize: 10
      },
      //已经存在的商品
      goods: [],
      //商品列表
      goodslist: [],
      addForm: [],
      //条数
      total: 0
    }
  },
  created() {
    this.getGoodsList()
    this.goodsList()
  },
  methods: {
    getRowKey(row) {
      return row.id
    },
    // 列表选择,val选中的表格行数据
    handleSelectionChange(val) {
      this.addForm = val
    },
    //获取已经有的商品
    async goodsList() {
      const { data: res } = await server.getSeckillGoods(this.$route.params.id)
      this.goods = res.data
    },
    //根据分页获取对应的商品列表
    async getGoodsList() {
      const { data: res } = await server.getSelGoods(this.queryInfo)

      if (res.code !== 200) {
        return this.$message.error('获取商品列表失败')
      }
      this.$message.success('获取商品列表成功')
      this.goodslist = res.data.data
      this.total = res.data.total
    },
    //监听分页改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    //监听页码值
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    async addGoods() {
      const { data: res } = await server.addSeckillGoods(
        this.$route.params.id,
        this.addForm
      )
      if (res.code !== 200) {
        return this.$message.error('添加失败')
      }
      this.goodsList()

      this.$message.success('添加成功')
    },
    remove(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.delete(id)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async delete(id) {
      const { data: res } = await server.delSeckillGoods(
        id,
        this.$route.params.id
      )

      if (res.code !== 200) {
        return this.$message.error('删除失败！')
      }
      //刷新列表
      this.goodsList()
      this.$message.success('删除成功！')
    }
  }
}
</script>
<style scoped>
</style>